<template>
  <div class="goods-item" @click="itemClick(product.id)">
    <img :src="product.cover_url" alt="" />
    <div class="goods-info">
      <p>{{ product.title }}</p>
      <span class="price"><small>$</small>{{ product.price }}</span>
      <span class="collect">{{ product.collects_count }}</span>
    </div>
  </div>
</template>

<script>
import { useRouter } from "vue-router";
export default {
  name: "GoodsListItem",
  setup() {
    const router = useRouter();
    const itemClick = (id) => {
      router.push({ path: "/detail", query: { id } });
    };

    return {
      itemClick,
    };
  },
  props: {
    product: {
      type: Object,
      default() {
        return {};
      },
    },
  },
};
</script>

<style lang='less'>
.goods-item {
  width: 46%;
  padding-bottom: 40px;
  position: relative;

  img {
    width: 100%;
    border-radius: 5px;
  }

  .goods-info {
    font-size: 12px;
    position: absolute;
    bottom: 5px;
    left: 0; //定位中分别让left right偏移都为0这样就居中了
    right: 0;
    overflow: hidden;
    text-align: center;
    p {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      margin-bottom: 3px;
    }

    .price {
      color: red;
      margin-right: 20px;
    }

    .collect {
      position: relative;
    }
    // ::before 表示在这个元素前面插入内容，content表示插入的内容
    .collect::before {
      content: "";
      position: absolute;
      left: -15px;
      top: -1px;
      width: 14px;
      height: 14px;
      background: url("@/assets/images/collect.png") 0 0/14px 14px;
    }
  }
}
</style>